define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $stateParams,Pointorder, GoPage, GoDialog, Auth, $ionicModal) {
		$scope.data = {
			delivery: []
		}

		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_page($stateParams.orderinfo);
		}, false);

		//初始化
		$scope.init_page = function(orderinfo) {
			try {
				orderinfo = window.atob(orderinfo);
				orderinfo = JSON.parse(orderinfo);
			} catch(ex) {
				orderinfo = null;
			}
			if (!orderinfo || !orderinfo.goods_id ||
				typeof(orderinfo.num) != "number" || isNaN(orderinfo.num) || orderinfo.num <= 0) {
				GoDialog.alert("提示",'画面参数异常',function(){
					GoPage.go_back();
				});
			}
			$scope.order_num = orderinfo.num;//商品数量
			Pointorder.get_detail({
				'goods_id': orderinfo.goods_id,
			}, function(res) {
//				console.log(res)
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				$scope.goods = res.result || {};
				if ($scope.goods.permission=='false') {
					GoDialog.toast("商品无法兑换");
				}

				var images = [];
				for (var imgid in $scope.goods.images) {
					images.push($scope.goods.images[imgid]);
				}
				$scope.goods.images = images;

				getAddressList();//初始化收货地址
			})
		}
		
		function getAddressList() {
			Pointorder.get_addresslist({
				pagelimit: 100,
				nPage: 0
			}, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				var addrs = res.result.addr || [];
				$scope.data.delivery = addrs;

				$scope.data.receiver = addrs[0] || {};//默认显示第一个
				for (var i=0; i<addrs.length; i++) {
					var region_id = addrs[i].region_id || "";
					if (region_id) {
						var i2 = region_id.indexOf(':');
						if (i2 > 0) {
							addrs[i].addr_area = region_id.substring(0, i2).replace(new RegExp('/', 'gm'), '');
						}
					}
					if (addrs[i].def_addr == 'true') {
						$scope.data.receiver = addrs[i];
					}
				}				
			})
		}
		
		/*********选择收货人和收货地址**********/
		$ionicModal.fromTemplateUrl("templates/modal/pointorder_delivery.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_delivery = modal;
		});
		//打开
		$scope.openModal_delivery = function() {
			if($scope.data.delivery.length>1){//2个及2个以上才显示弹窗
				$scope.modal_delivery.show();
			}
		}
		//隐藏
		$scope.closeModal_delivery = function() {
			$scope.modal_delivery.hide();
		}
		//选择收货人
		$scope.selectDelivery = function(index){
			$scope.deliveryList_index = index;
		}
		//选择收货人和收货地址
		$scope.getDelivery = function() {
			$scope.closeModal_delivery();
			$scope.data.receiver = $scope.data.delivery[$scope.deliveryList_index];
		}
		/*********选择收货人和收货地址**********/
		
		$scope.submit_order = function() {
			var rec = $scope.data.receiver;
			if (!rec) {
				GoDialog.toast("请选择收货人和收货地址");
				return;
			}

			var delivery = {
				"addr_id": rec.addr_id,
				"ship_area": rec.region_id,
				"ship_addr_area": undefined,
				"ship_addr": rec.addr,
				"ship_zip": rec.zip,
				"ship_name": rec.name,
				"ship_mobile": rec.mobile
			}

			if (rec.region_id) {
				var i = rec.region_id.indexOf(":");
				if (i >= 0) {
					var area = rec.region_id.substring(0, i);
					delivery.ship_addr_area = area.replace(new RegExp('/', 'gm'), '');
				}
			}

			var g = $scope.goods || {};
			var business_goods = {};
			business_goods[g.goods.store_id] = "goods_" + g.goods_id + "_" + g.product_id;
			
			Pointorder.create_order({
				'delivery': JSON.stringify(delivery),
				'payment': '{"pay_app_id":"scorepay"}',
				'shipping': '[[{"shipping_id":"132"}]]',
				'business_goods': JSON.stringify(business_goods)
			}, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				GoDialog.toast('兑换成功');
				GoPage.go_states("afall", { "type": 2 });
			})
		}
		
		
	}

	controller.$inject = ['$scope', '$stateParams','Pointorder', 'GoPage', 'GoDialog', 'Auth','$ionicModal'];
	
	return app.registerController("pointorderctrl",controller);
});